package control_centre

import (
	"encoding/json"
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	_ "github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	_ "github.com/hajimehoshi/ebiten/v2/ebitenutil"
	// 导入jpeg-用于解码jpg文件
	_ "image/jpeg"
	"log"
	"os"
)

type Config struct {
	ScreenWidth   int     `json:"screenWidth"`
	ScreenHeight  int     `json:"screenHeight"`
	Title         string  `json:"title"`
	ZombieSpeed   float64 `json:"zombieSpeed"`
	ZombieNumbers int     `json:"zombieNumbers"`

	PeaShooterAttackFrequency int `json:"peaShooterAttackFrequency"`
}

func LoadConfig() *Config {
	f, err := os.Open("PlantsVsZombies1/config/config.json")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	var cfg Config
	err = json.NewDecoder(f).Decode(&cfg)
	fmt.Println("json文件加载完毕")
	return &cfg
}

func LoadImage() map[string]*ebiten.Image {
	var imagesLoad = make(map[string]*ebiten.Image)
	// 游戏背景
	img, _, err := ebitenutil.NewImageFromFile("PlantsVsZombies1/image/interface/background1.jpg")
	if err != nil {
		log.Fatal(err)
	}
	imagesLoad["background1"] = img
	// 卡槽背景
	img, _, err = ebitenutil.NewImageFromFile("PlantsVsZombies1/image/卡槽/ChooserBackground.png")
	if err != nil {
		log.Fatal(err)
	}
	imagesLoad["card_slot"] = img
	fmt.Println("背景/卡槽...植物作战场景加载完毕")
	return imagesLoad
}

func Load_select_plant_card(images map[string]*ebiten.Image) {
	// 豌豆射手
	img, _, err := ebitenutil.NewImageFromFile("PlantsVsZombies1/image/卡槽/card_1.png")
	if err != nil {
		log.Fatal(err)
	}
	images["peashooter"] = img
	PlantCardSlice = append(PlantCardSlice, "peashooter")
	// 豌豆射手
	img, _, err = ebitenutil.NewImageFromFile("PlantsVsZombies1/image/卡槽/card_2.png")
	if err != nil {
		log.Fatal(err)
	}
	images["sunFlower"] = img
	PlantCardSlice = append(PlantCardSlice, "sunFlower")
	// 樱桃炸弹
	img, _, err = ebitenutil.NewImageFromFile("PlantsVsZombies1/image/卡槽/card_4.png")
	if err != nil {
		log.Fatal(err)
	}
	images["cherryBomb"] = img
	PlantCardSlice = append(PlantCardSlice, "cherryBomb")
	// 坚果墙
	img, _, err = ebitenutil.NewImageFromFile("PlantsVsZombies1/image/卡槽/card_wallnut.png")
	if err != nil {
		log.Fatal(err)
	}
	images["nutWall"] = img
	PlantCardSlice = append(PlantCardSlice, "nutWall")
	fmt.Println("植物卡片加载完毕")
}
